﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.ObjectModel;
using University.Workshop.Model;
using University.Workshop.Business;
using System.Globalization;

namespace University.Workshop.WebApp
{
    public partial class AdvancedSearch : System.Web.UI.Page
    {
        public static Collection<Product> products = new Collection<Product>();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                try
                {
                    LblError.Text = "";
                    products = DataAccess.ProductDataAccess.Products();
                    TopicGrid.DataSource = products;
                    DataBind();
                }
                catch (Exception er)
                {
                    LblError.Text = er.Message;
                }
            }
            LblResults.Text = products.Count.ToString() + " Results";
        }

        protected void BtnSearch_Click(object sender, EventArgs e)
        {
            String currency="";
            TopicGrid.CurrentPageIndex = 0;
            
            if (!noCurrency.Checked)
            {
                
                if (Bolivianos.Checked)
                {
                    currency = "Bolivianos";
                    
                }
                else
                {
                    if (Dollars.Checked)
                    {
                        currency = "Dollars";
                       
                    }
                }
            }
            try
            {
                LblError.Text = "";
                
                products = SearchAdvancedProduct.FilteringFields(DropDownList1.SelectedItem.Text, TxtBoxPrice1.Text, TxtBoxPrice2.Text, currency, TxtBoxQuantity1.Text, TxtBoxQuantity2.Text, Calendar1.SelectedDate.ToString(), Calendar2.SelectedDate.ToString(), TxtBoxWords.Text);
                TopicGrid.DataSource = products;
                DataBind();
                LblResults.Text = products.Count.ToString()+" Results";
            }
            catch (Exception er)
            {
                LblError.Text = er.Message;
            }
        }

        protected void OnPageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            TopicGrid.CurrentPageIndex = e.NewPageIndex;
            TopicGrid.DataSource = products;
            TopicGrid.DataBind();
        }

        protected void TopicGrid_EditCommand(object source, DataGridCommandEventArgs e)
        {
            int productSelected = e.Item.ItemIndex + (TopicGrid.CurrentPageIndex * TopicGrid.PageSize);
            string productId = products[productSelected].ProductId.ToString();
            string name = products[productSelected].Name.ToString();
            string price = products[productSelected].Price.ToString();
            string category = products[productSelected].Category.ToString();
            Response.Redirect("ProductSelected.aspx?ProductID=" +productId+ "&ProductName=" + name + "&ProductPrice=" +price + "&ProductCategory=" + category);
        }
    }
}